 <template>
  <el-card style="width: 500px;margin: 10% auto auto;">
    <el-form label-width="120px" size="small" :model="form" :rules="rules" ref="pass">
      <el-form-item label="原密码" prop="password">
        <el-input v-model="form.password" autocomplete="off" show-password></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input v-model="form.newPassword" autocomplete="off" show-password></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="confirmPassword">
        <el-input v-model="form.confirmPassword" autocomplete="off" show-password></el-input>
      </el-form-item>
      <el-form-item style="float:right;">
        <el-button type="primary" @click="this.$router.back()">取 消</el-button>
        <el-button type="primary" @click="reset">提 交</el-button>

      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import request from "@/utils/request";


export default {
  name: "ResetPassword",
  data() {
    return {
      form: {},
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
      rules: {
        password: [
          { required: true, message: '请输入原密码', trigger: 'blur' },
          { min: 3, message: '长度不少于3位', trigger: 'blur' }
        ],
        newPassword: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { min: 3, message: '长度不少于3位', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, message: '长度不少于3位', trigger: 'blur' }
        ],
      }
    }
  },
  created() {
    this.form.username = this.user.username
    this.form.id  = localStorage.getItem("uid")
  },
  methods: {
    reset() {

      this.$refs.pass.validate((valid) => {
        if (valid) {
          if (this.form.newPassword !== this.form.confirmPassword) {
            this.$message.error("2次输入的新密码不相同")
            return false
          }
          this.form.oldPassword = this.form.password
         request.post("http://124.70.179.188:8082/tea/changePwdTea", this.form).then(res => {
            if (res.success === true) {
              this.$message.success("修改成功")
              this.$store.commit("logout")
            } else {
              this.$message.error(res.message)
            }
          })
        }
      })
    },

  }
}
</script>
